/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} sum
 * @return {boolean}
 */
var hasPathSum = function(root, sum) {
    if(sum===0&&!root)return false;

    return helper(root, sum);


};

function helper(root, sum) {
    if(!root)return false;
    sum = sum - root.val;
    if(!root.left&&!root.right){
        if(sum!==0)return false;
        else return true;
    }else{
        return helper(root.left, sum) || helper(root.right, sum);
    }
}